#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/in.h>
// 客户端发送
int main(int argc, char const *argv[])
{
    /* code */
    struct sockaddr_in svr_addr;
    char sbuffer[1024] = {0};
    char rbuffer[1024] = {0};
    ssize_t sbytes = 0, rbytes = 0;
    int sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd == -1) {
        perror("error socket()");
        exit(EXIT_FAILURE);
    }
    printf("sockfd = %d\n", sockfd);
    bzero(&svr_addr, sizeof(svr_addr)); // 清空内存区域


    svr_addr.sin_family = AF_INET;
    svr_addr.sin_port = htons(atoi(argv[2]));
    svr_addr.sin_addr.s_addr = inet_addr(argv[1]);

    if (connect(sockfd, (struct sockaddr *)&svr_addr, sizeof(svr_addr)) == -1) {
        perror("error connect()");
        exit(EXIT_FAILURE);
    }
    // 先建立连接再发送数据
    strcpy(sbuffer, "hello world");
    sbytes = send(sockfd, sbuffer, strlen(sbuffer) + 1, 0);
    if (sbytes == -1) {
        perror("error send()");
        exit(EXIT_FAILURE);
    }
    close(sockfd);
    return 0;
}
